<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

define("MODULE_LIBRARY_CLASSES", "module/");

require_once MODULE_LIBRARY_CLASSES . 'Module.php';

class ModuleLoader
{
    private $modules = array();
    private $CI;
	
/*
|===============================================================================
| Constructeur
|===============================================================================
*/
	
	public function __construct()
	{
            $this->CI =& get_instance();
            //$this->checkRepository();
	}
        
        public function getModules(){
            return $this->modules;
        }

        public function getRepositoryPath(){
            return './' . APPPATH . '/modules';
        }

        private function checkRepository(){
            $this->CI->load->helper('directory');            
            $map = directory_map(MODULE_REPOSITORY);
            foreach ($map as $k => $v)
            {
                if (is_array($v)){
                    $this->addModule($k);
                }   
            }
        }
        
        public static function getModulesDB(){
            $db = get_instance()->db;         
            $query = $db->get('module');
            $modules = array();
            
            foreach ($query->result() as $result)
            {
                $modules[$result->name] = new Module($result);
            }
            
            return $modules;
        }
        
        public static function getModuleRepository(){
            get_instance()->load->helper('directory');            
            $map = directory_map(MODULE_REPOSITORY);
            $modules = array();
            foreach ($map as $k => $v)
            {
                if (is_array($v)){
                    $modules[$k] = new Module_File($k);
                }   
            }
            
            return $modules;
        }
        
        private function addModule($name){
            $this->modules[$name] = new Module_File($name);
        }
        
        public static function getModule($name){
            $query = &get_instance()->db->query("SELECT * FROM module WHERE name='" . $name . "';");
            $module = $query->_fetch_object();

            return $module;
        }
}

/* End of file layout.php */
/* Location: ./application/libraries/layout.php */

